#define  _CRT_SECURE_NO_WARNINGS 1

using namespace std;
#include<iostream>

struct ListNode {
	int val;
	ListNode* next;
	ListNode() : val(0), next(nullptr) {}
	ListNode(int x) : val(x), next(nullptr) {}
	ListNode(int x, ListNode* next) : val(x), next(next) {}
	
};

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        if (l1 == nullptr || l2 == nullptr)
        {
            if (l1 == nullptr)
                return l2;
            if (l2 == nullptr)
                return l1;
        }

        if (l1 == nullptr && l2 == nullptr)
        {
            ListNode* newnode = new ListNode(0);
            return newnode;
        }

        if ((l1->val == 0 && l2->val == 0) && (l1->next == nullptr && l2->next == nullptr))
        {
            ListNode* newnode = new ListNode(0);
            return newnode;
        }

        ListNode* cur1 = l1;
        long long i = 1;
        long long sum1 = 0;
        while (cur1)
        {
            sum1 += (cur1->val * i);
            i *= 10;
            cur1 = cur1->next;
        }

        ListNode* cur2 = l2;
        long long j = 1;
        long long sum2 = 0;
        while (cur2)
        {
            sum2 += (cur2->val * j);
            j *= 10;
            cur2 = cur2->next;
        }

        long long sum = sum1 + sum2;

        ListNode* Node = new ListNode(0);
        ListNode* cur = Node;

        while (sum)
        {
            int a = sum % 10;
            ListNode* newnode = new ListNode(a);
            cur->next = newnode;
            cur = cur->next;
            sum /= 10;
        }

        return Node->next;
    }
};

int main()
{
    Solution s;
    ListNode* l1=new ListNode(7);
    ListNode* l11=new ListNode(1);
    ListNode* l12=new ListNode(6);
    l1->next = l11;
    l11->next = l12;
    l12->next = nullptr;

    ListNode* l2 = new ListNode(5);
    ListNode* l21 = new ListNode(9);
    ListNode* l22 = new ListNode(2);
    l2->next = l21;
    l21->next = l22;
    l22->next = nullptr;

    s.addTwoNumbers(l1, l2);


    return 0;
}